﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using FinPlusAssembler;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValuePod : FinPlusControl
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Test = true)]
        public override string TestString { get { return string.Empty; } }
        [State(Test = true)]
        public string ViewString { get { return  string.Empty; } }
        
        public string Pod { get { return GetValue(PodProperty).ToString(); } set { SetValue(PodProperty, value); } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }
        public static readonly DependencyProperty PodProperty = DependencyProperty.Register(Propertys.Pod.ToString(), typeof(string), typeof(FinPlusValuePod), new UIPropertyMetadata(Callback));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValuePod));

        private bool _hasSetEnable;
        private enum Propertys { Pod, IsEnabled }

        //construct
        public FinPlusValuePod()
        {
            InitializeComponent();
            IsValid = true;
        }

        //common control interface
        public override void CmdExecute(IPod command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.Open: Open(); break;
                    default: throw new Exception(string.Format("cmd not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = Level.Warning.Log(Id, Config, e.Message, e);
            }
        }

        //public
        public void SetControl()
        {
            //permission
            if (!_hasSetEnable)
            {
                 button.IsEnabled = IsEnabled;
                _hasSetEnable = true;
            }
        }

        //private
        private void Open()
        {
            var inject = new object[,] { { "Pod", IOC.New<IPod>((object[,])Pod.ToArray2d()) } };
            new FinPlusWindowHelper().OpenAsync(Paths.Values["FolderSubViews"] + "PodEditor", "PodEditor", inject, Env, ShowType.Dispatch, "", "", true);
        }

        //cmds
        private enum Cmds { Open }

        //events
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValuePod)d).SetControl(); }
    }
}